package com.yilin.platform.service.impl.truck;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yilin.platform.base_core.application.PageData;
import com.yilin.platform.base_core.commons.entity.AuthStatus;
import com.yilin.platform.base_core.commons.utils.ParamUtil;
import com.yilin.platform.base_core.tools.QueryUtil;
import com.yilin.platform.entity.truck.TravelLicense;
import com.yilin.platform.entity.truck.TruckVehicle;
import com.yilin.platform.entity.user.Account;
import com.yilin.platform.mapper.truck.TravelLicenseMapper;
import com.yilin.platform.service.impl.user.AccountService;
import com.yilin.platform.service.truck.ITravelLicenseService;
import com.yilin.platform.service.truck.ITruckVehicleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.Date;

/**
 * 行驶证服务
 *
 * @author zhangjinyuan
 * @date 2022/11/11
 */
@Service
@Transactional
public class TravelLicenseService extends ServiceImpl<TravelLicenseMapper, TravelLicense> implements ITravelLicenseService {

    @Resource
    ITruckVehicleService vehicleService;

    @Resource
    AccountService accountService;
    @Resource
    TravelLicenseMapper travelLicenseMapper;



    /**
     * 获取行驶证分页
     */
    @Override
    public PageData<TravelLicense> getTravelLicensePage(Account loginUser, TravelLicense query, Integer start, Integer limit) {
        QueryWrapper<TravelLicense> queryWrapper = new QueryWrapper<>();
        QueryUtil.buildOrganizeIdOrSuperiorPlatIdWrapper(queryWrapper,loginUser);
        QueryUtil.andQueryWrapper(queryWrapper, query);
        return page(new PageData<>(start, limit), queryWrapper);
    }

    /**
     * 保存行驶证
     */
    @Override
    public void saveTravelLicense(Account user, TravelLicense travelLicense) {
        ParamUtil.isNull(travelLicense.getTravelPhotoPositiveImg(), "行驶证正面图为空");
        ParamUtil.isNull(travelLicense.getTravelPhotoBackImg(), "行驶证背面图为空");
        ParamUtil.isNull(travelLicense.getUsageCharacteristics(), "使用性质为空");
        ParamUtil.isNull(travelLicense.getIssuingOrganizations(), "发证机关为空");
        ParamUtil.isNull(travelLicense.getVehicleIdentifyCode(), "车辆识别代码为空");
        ParamUtil.isNull(travelLicense.getRegisterDate(), "注册日期为空");
        ParamUtil.isNull(travelLicense.getTruckEnergyTypeId(), "车辆能源类型为空");
        ParamUtil.isNull(travelLicense.getTruckTypeId(), "车辆类型为空");
        ParamUtil.isNull(travelLicense.getVehicleLong(), "车长为空");
        ParamUtil.isNull(travelLicense.getVehicleHeight(), "车高为空");
        ParamUtil.isNull(travelLicense.getVehicleWidth(), "车宽为空");
        ParamUtil.isNull(travelLicense.getVehicleWeight(), "车辆总质量为空");
        ParamUtil.isNull(travelLicense.getLoadingWeight(), "车辆载重为空");
        ParamUtil.isNull(travelLicense.getTruckNo(), "车牌号为空");
        travelLicense.setAuthStatus(AuthStatus.waitProcess);
        travelLicense.setOrganizeId(user.getOrganizeId());
        travelLicense.setOrganizeName(user.getOrganizeName());
        if (user.getSuperiorPlatId() != null) {
            travelLicense.setSuperiorPlatId(user.getSuperiorPlatId());
            travelLicense.setSuperiorPlatName(user.getSuperiorPlatName());
        }
        if (travelLicense.getId() == null) {
            travelLicense.setAddUserId(user.getId());
            travelLicense.setCreateTime(new Date());
            save(travelLicense);
        } else {
            travelLicense.setUpdateUserId(user.getId());
            travelLicense.setUpdateTime(new Date());
            updateById(travelLicense);
        }
    }

    /**
     * 审核并更新行驶证数据
     */
    @Override
    public void verifyAndUpdateAuth(Account loginUser, TravelLicense travelLicense) {
        TruckVehicle truckVehicle = vehicleService.getById(travelLicense.getVehicleId());
//        Organize organize = organizeService.getOrganizeById(truckVehicle.getOrganizeId());
//        Account account = accountService.getAccountById(organize.getManagerUserId());
        travelLicense.setAutTime(new Date());
        travelLicense.setAutUserId(loginUser.getId());
        travelLicense.setAutUserName(loginUser.getName());
        travelLicense.setUpdateTime(new Date());
        travelLicense.setUpdateUserId(loginUser.getId());
        if (travelLicense.getAuthStatus() != AuthStatus.authSuccess) {
//            String text = String.format("您的从业资格证认证失败，请尽快登录APP重新认证。失败原因:%s",
//                    travelLicense.getFailedMsg());
////            ParamUtil.isSuccess(messageRemote.sendSystemNotify(account.getId(), text, null));
////            messageRemote.sendPhoneMessage(account.getPhone(), text);
        } else {
            truckVehicle.setTrackNo(travelLicense.getTruckNo());
            //车辆核定数据
            truckVehicle.setVehicleWeight(travelLicense.getVehicleWeight());
            truckVehicle.setLoadingWeight(travelLicense.getLoadingWeight());
            truckVehicle.setVehicleHeight(travelLicense.getVehicleHeight());
            truckVehicle.setVehicleWidth(travelLicense.getVehicleWidth());
            truckVehicle.setVehicleLong(travelLicense.getVehicleLong());
            //车辆类型数据
            truckVehicle.setTruckTypeId(travelLicense.getTruckTypeId());
            truckVehicle.setTruckTypeCode(travelLicense.getTruckTypeCode());
            truckVehicle.setTruckTypeName(travelLicense.getTruckTypeName());
            //车辆能源类型
            truckVehicle.setTruckEnergyTypeId(travelLicense.getTruckEnergyTypeId());
            truckVehicle.setTruckEnergyTypeCode(travelLicense.getTruckEnergyTypeCode());
            truckVehicle.setTruckEnergyTypeName(travelLicense.getTruckEnergyTypeName());
            truckVehicle.setTruckStatus(TruckVehicle.TruckStatus.unused);
//            String TravelLicenseText = "您的行驶证认证成功，您现在可以登录APP进行下一步操作了~";
////            ParamUtil.isSuccess(messageRemote.sendSystemNotify(account.getId(), TravelLicenseText, null));
////            messageRemote.sendPhoneMessage(account.getPhone(), TravelLicenseText);
        }
        updateById(travelLicense);
        vehicleService.updateById(truckVehicle);
    }

    @Override
    public TravelLicense getTravelLicenseByTruckId(String truckId){
        QueryWrapper<TravelLicense> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("vehicle_id", truckId);
        return travelLicenseMapper.selectOne(queryWrapper);
    }

}
